home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 September / macformat-041.iso / mac / Shareware City / Graphics / MacSPD / Sources / lattice.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-30  |  4.3 KB  |  154 lines  |  [TEXT/MMCC]

  1. /*
  2.  * lattice.c - Create a set of shiny spheres, with each sphere connected to
  3.  *         its neighbors by matte cylinders, forming a cubic lattice.
  4.  *    Six light sources.
  5.  *
  6.  * Author:  Antonio Costa, INESC-Norte.
  7.  * Version:  3.1 (04/09/93)
  8.  *
  9.  * SizeFactor determines the number of objects output.
  10.  *    Total spheres   = (SF+1)**3.
  11.  *    Total cylinders = 3*(SF**3)+...
  12.  *
  13.  *    SizeFactor    # spheres    # cylinders
  14.  *         1            8             12
  15.  *         2            27             54
  16.  *         3            64            144
  17.  *
  18.  *         5           216            540
  19.  *
  20.  *        12           2197            6084
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <math.h>
  25. #include <stdlib.h>    /* atoi */
  26. #include "def.h"
  27. #include "drv.h"        /* display_close() */
  28. #include "lib.h"
  29.  
  30. /* These may be read from the command line */
  31. static int size_factor        = 12;
  32. static int raytracer_format = OUTPUT_RT_DEFAULT;
  33. static int output_format    = OUTPUT_CURVES;
  34.  
  35.  
  36. #define RADIUS1 (1.0 / 4.0)
  37. #define RADIUS2 (1.0 / 16.0)
  38.  
  39. #define inv_factor (1.0 / (double) size_factor)
  40. #define radius1 ((double) RADIUS1 / (double) size_factor)
  41. #define radius2 ((double) RADIUS2 / (double) size_factor)
  42.  
  43. main(argc, argv)
  44.     int        argc;
  45.     char    *argv[];
  46. {
  47.     COORD4    back_color, obj_color;
  48.     COORD4    light;
  49.     COORD4    from, at, up;
  50.     COORD4    center, center1, center2;
  51.     long    x, y, z;
  52.     double    delta, x0, y0, z0, lscale;
  53.  
  54.     PLATFORM_INIT(SPD_LATTICE);
  55.  
  56.     /* Start by defining which raytracer we will be using */
  57.     if ( lib_gen_get_opts( argc, argv,
  58.             &size_factor, &raytracer_format, &output_format ) ) {
  59.     return EXIT_FAIL;
  60.     }
  61.     if ( lib_open( raytracer_format, "Lattice.out" ) ) {
  62.     return EXIT_FAIL;
  63.     }
  64.  
  65.     delta =
  66.     (radius1 * (1.0 - sqrt((double) RADIUS2 / (double) RADIUS1))) * 0.99;
  67.  
  68.     /* output background color - UNC sky blue */
  69.     /* NOTE: Do this BEFORE lib_output_viewpoint(), for display_init() */
  70.     SET_COORD3(back_color, 0.078, 0.361, 0.753);
  71.     lib_output_background_color(back_color);
  72.  
  73.     /* output viewpoint */
  74.     SET_COORD3(from, ((double) (size_factor >> 1) + 0.5) * inv_factor, 1.0,
  75.              1.0 - 1.0 / (double) (size_factor << 1));
  76.     SET_COORD3(at, ((double) (size_factor >> 1) + 0.5) * inv_factor, -1.0,
  77.              -1.0 - 1.0 / (double) (size_factor << 1));
  78.     SET_COORD3(up, 0.2, 1.0, 0.0);
  79.     lib_output_viewpoint(from, at, up, 60.0, 1.0, 0.0, 512, 512);
  80.  
  81.     /* output light sources */
  82.     lscale = 1.0 / sqrt(6.0);
  83.  
  84.     SET_COORD4(light, 2.0, 0.5, 0.5, lscale);
  85.     lib_output_light(light);
  86.     SET_COORD4(light, -1.0, 0.5, 0.5, lscale);
  87.     lib_output_light(light);
  88.     SET_COORD4(light, 0.5, 2.0, 0.5, lscale);
  89.     lib_output_light(light);
  90.     SET_COORD4(light, 0.5, -1.0, 0.5, lscale);
  91.     lib_output_light(light);
  92.     SET_COORD4(light, 0.5, 0.5, 2.0, lscale);
  93.     lib_output_light(light);
  94.     SET_COORD4(light, 0.5, 0.5, -1.0, lscale);
  95.     lib_output_light(light);
  96.  
  97.     for (x = 0; x <= size_factor; x++) {
  98.     x0 = (double) x / (double) size_factor;
  99.     for (y = 0; y <= size_factor; y++) {
  100.         y0 = (double) y / (double) size_factor;
  101.         for (z = 0; z <= size_factor; z++) {
  102.  
  103.         PLATFORM_MULTITASK();
  104.  
  105.         z0 = (double) z / (double) size_factor;
  106.  
  107.         SET_COORD3(obj_color, 0.9, 0.9, 0.9);
  108.         lib_output_color(NULL, obj_color,
  109.             0.0, 0.5, 0.5, 50.0, 5.0, 0.0, 0.0);
  110.  
  111.         SET_COORD4(center, x0, y0, z0, radius1);
  112.         lib_output_sphere(center, output_format);
  113.  
  114.         if (x != size_factor) {
  115.             SET_COORD3(obj_color, 0.9, 0.1, 0.1);
  116.             lib_output_color(NULL, obj_color,
  117.                 0.1, 0.99, 0.0, 0.0, 0.0, 0.0, 0.0);
  118.  
  119.             SET_COORD4(center1, x0 + delta, y0, z0, radius2);
  120.             SET_COORD4(center2, x0 + inv_factor - delta, y0, z0,
  121.                 radius2);
  122.             lib_output_cylcone(center1, center2, output_format);
  123.         }
  124.         if (y != size_factor) {
  125.             SET_COORD3(obj_color, 0.1, 0.9, 0.1);
  126.             lib_output_color(NULL, obj_color,
  127.                 0.1, 0.99, 0.0, 0.0, 0.0, 0.0, 0.0);
  128.  
  129.             SET_COORD4(center1, x0, y0 + delta, z0, radius2);
  130.             SET_COORD4(center2, x0, y0 + inv_factor - delta, z0,
  131.                 radius2);
  132.             lib_output_cylcone(center1, center2, output_format);
  133.         }
  134.         if (z != size_factor)
  135.         {
  136.             SET_COORD3(obj_color, 0.1, 0.1, 0.9);
  137.             lib_output_color(NULL, obj_color,
  138.                 0.1, 0.99, 0.0, 0.0, 0.0, 0.0, 0.0);
  139.  
  140.             SET_COORD4(center1, x0, y0, z0 + delta, radius2);
  141.             SET_COORD4(center2, x0, y0, z0 + inv_factor - delta,
  142.                 radius2);
  143.             lib_output_cylcone(center1, center2, output_format);
  144.         }
  145.         }
  146.     }
  147.     }
  148.  
  149.     lib_close();
  150.  
  151.     PLATFORM_SHUTDOWN();
  152.     return EXIT_SUCCESS;
  153. }
  154.